package glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.Target;
import com.example.common.R;

import java.io.File;

import utils.StringUtils;

/**
 * Description : 图片加载工具类 使用glide框架封装
 */
public class ImageLoaderUtils {
    /**
     * 自定义占位图
     *
     * @param context
     * @param imageView
     * @param url
     * @param placeholder
     * @param error
     */

    public static void display(Context context, ImageView imageView, String url, int placeholder, int error) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url).placeholder(placeholder)
                .error(error).crossFade().into(imageView);
    }

    /**
     * 无占位图 支持网络链接
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void display(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        if (StringUtils.isNull(url)) {//无图片加载空的
            Glide.with(context).load(R.mipmap.bg_loading_none)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .placeholder(R.mipmap.bg_square_ing)
                    .error(R.mipmap.bg_square_error)
                    .crossFade().into(imageView);
        }else {
            //防止图片变形
            Glide.with(context)
                    .load(url)
                    .asBitmap().
                    centerCrop()
                    .placeholder(R.mipmap.bg_square_ing)
                    .error(R.mipmap.bg_square_error)
                    .into(new MyBitmapImageViewTarget(imageView));
        }
    }

    /**
     * 无占位图 支持File
     *
     * @param context
     * @param imageView
     * @param url
     */

    public static void display(Context context, ImageView imageView, File url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .placeholder(R.mipmap.bg_square_ing)
                .error(R.mipmap.bg_square_error)
                .crossFade().into(imageView);
    }

    /**
     * 为了兼容圆形控件
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void displayCircleUrl(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .transform(new GlideCircleTransfromUtil(context))
                .placeholder(R.mipmap.bg_square_ing)
                .error(R.mipmap.bg_square_error)
                .into(new MyBitmapImageViewTarget(imageView));
    }

    /***
     * 本地图片为圆形
     *
     * @param context
     * @param imageView
     * @param path
     */
    public static void displayLocal(Context context, ImageView imageView, String path) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load("file://" + path)
                .asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .transform(new GlideCircleTransfromUtil(context))
                .placeholder(R.mipmap.bg_square_ing)
                .error(R.mipmap.bg_square_error)
                .into(new MyBitmapImageViewTarget(imageView));
    }

    /**
     * @param context
     * @param imageView
     * @param url
     */
    public static void displaySmallPhoto(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url).asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.bg_square_ing)
                .error(R.mipmap.bg_square_error)
                .thumbnail(0.5f)
                .into(imageView);
    }

    /**
     * 加载质量更高的图片
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void displayBigPhoto(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url).asBitmap()
                .format(DecodeFormat.PREFER_ARGB_8888)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.bg_square_ing)
                .error(R.mipmap.bg_square_error)
                .into(imageView);
    }

    /**
     * 加载本地的资源图片
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void display(Context context, ImageView imageView, int url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .placeholder(R.mipmap.bg_square_ing)
                .error(R.mipmap.bg_square_error)
                .crossFade().into(imageView);
    }

    /**
     * 加载圆角
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void displayRound(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.toux2)
                .centerCrop().transform(new GlideRoundTransformUtil(context)).into(imageView);
    }

    /**
     * 加载圆角
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void displayRound(Context context, ImageView imageView, int url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.toux2)
                .centerCrop().transform(new GlideRoundTransformUtil(context)).into(imageView);
    }

    /**
     * 自适应宽度加载图片。保持图片的长宽比例不变，通过修改imageView的高度来完全显示图片。
     */
    public static void loadIntoUseFitWidth(Context context, final String imageUrl, final ImageView imageView) {
        Glide.with(context)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        if (imageView == null) {
                            return false;
                        }
                        if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
                        int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        float scale = (float) vw / (float) resource.getIntrinsicWidth();
                        int vh = Math.round(resource.getIntrinsicHeight() * scale);
                        params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                        imageView.setLayoutParams(params);
                        return false;
                    }
                })
                .dontAnimate()
                .placeholder(R.mipmap.bg_square_error)
                .error(R.mipmap.bg_square_error)
                .into(imageView);
    }

    /**
     * 加载圆形图片
     * @param context 上下文
     * @param url url
     * @param imageView view
     */
    public static void loadingCircle(final Context context,String url,final ImageView imageView) {
        Glide.with(context).load(url).asBitmap().centerCrop().into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    /**
     * 加载圆角
     * @param mContext c
     * @param mUrl url
     * @param mIv iv
     * @param radius 圆角
     */

    public static void doLoadRoundFillet(Context mContext,String mUrl,ImageView mIv,int radius){
        Glide.with(mContext).load(mUrl).transform(new GlideRoundTransform(mContext,radius)).into(mIv);
    }

}
